<?php

/**
 * Contrôleur des villes
 */
class ville_controller extends Component
{
    // Attributs
    const byPage = 10;
    const page_identifier = 'page-';
    public $_pageTotal;

    /**
     * Constructeur
     */
    public function __construct()
    {
    }

    /**
     * Données à envoyer à la vue par défaut
     */
    public function defaut()
    {
        if (isset($_GET['param'][0])) {
            if (preg_match("/".self::page_identifier."/i", $_GET['param'][0])) {
                $p = explode(self::page_identifier, $_GET['param'][0]);
                $this->getAll($p[1]);
            } else {
                $tmp = explode('-',$_GET['param'][0]);
                $this->getOne((int)$tmp[0]);
            }
        } else {
            $this->getAll();
        }
    }

    /**
     * Classement des villes
     */
    public function classement(){
        $datas = Base::Load(CLASS_RELEVE)->getVilleLessCost(500);
        $this->_view->assign('data', $datas);
        $this->_view->addBlock('content', 'listClassement.tpl');
    }

    /**
     * Sélection d'une ville
     *
     * @param unsigned $id L'id d'une ville
     */
    private function getOne($id)
    {
        $data       = Base::Load(CLASS_LOC)->getVille($id);
        $coms       = Base::Load(CLASS_COMMENTAIRE)->findBy('object_id', (string) $id, true);
        $userTMP    = Base::Load(CLASS_USER)->findBy('ville',(string)$id);
        $ShopsList  = BASE::Load(CLASS_SHOP)->findBy('ville',(string)$id);
        
        $releve = Base::Load(CLASS_RELEVE)->getByVille($id);
        $this->_view->assign('releves', $releve);
        if(is_object($userTMP)){
            foreach ($userTMP as $tmp) {
                if (isset($tmp['lat']) && isset($tmp['lng'])) {
                    $users[] = $tmp;
                }
            }
            $this->_view->assign('chasseurs', $users);
        }
        
        $this->_view->assign('shops', $ShopsList);

        $this->_view->assign('coms', $coms);
        $this->addBreadCrumb($data[2]);
        $this->_view->assign('data', $data);
        $this->_view->assign('user_connected', Base::Load(CLASS_USER)->isConnect());
        $this->_view->addBlock('content', 'ville.tpl');
    }

    /**
     * Récupération de toutes les villes
     *
     * @param unsigned $page L'endroit du tableau à visualiser (pagination)
     */
    private function getAll($page = 1)
    {
        $this->_view->addBlock('autoCompleteVille','formAutocomplete.tpl');

        $dataTmp = Base::Load(CLASS_LOC)->getVille();
        $this->_pageTotal = ceil(count($dataTmp) / self::byPage) + 1;
        $start = self::byPage * $page - self::byPage;
        $pageStart = $page - 5;

        if ($pageStart < 1) {
            $pageStart = 1;
        }

        foreach ($dataTmp as $i => $d) {
            if ($i >= $start) {
                if ($i < $start + self::byPage) {
                    $d[1] = Base::Load(CLASS_LOC)->getDepartement($d[1]);
                    $data[$i] = $d;
                } else {
                    break;
                }
            }
        }

        $this->_view->assign('pageTotal', $this->_pageTotal);
        $this->_view->assign('page', $page);
        $this->_view->assign('pageStart', $pageStart);
        $this->_view->assign('pageStop', $pageStart + self::byPage - 1);
        $this->_view->assign('data', $data);

        if (isset($_GET['ajax']) && $_GET['ajax']) {
            $this->_view->assign('ajax', true);
            Base::Load(CLASS_BASE)->setLayout('vide.tpl');
            $this->_view->addBlock('data', 'list.tpl');
        } else {
            $this->_view->addBlock('content', 'list.tpl');
        }
    }

    public function getSelect()
    {
        $dataTmp = Base::Load(CLASS_LOC)->getVilleByDepartementID($_GET['param'][0]);
        exit(json_encode($dataTmp));
    }

    public function getAutoComplete()
    {
        $this->setContentType('text/plain');
        $query = $_GET['query'];
        $queryType = array('ville');
        $data = Base::Load(CLASS_SEARCH)->MySearch($query, $queryType, true);
        exit($data);
    }

    public function POST_Commentaire(array $data)
    {
        if (!empty($data['message'])) {
            $data['user'] = $_SESSION['user']['uid'];
            Base::Load(CLASS_COMMENTAIRE)->save($data);
            header('Location: ' . HTTP_HOST_REQUEST);
        }
    }
}


/* -- Fin du fichier -- */